#include <GUIConstantsEx.au3>
#include 'CommMG.au3'

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================
Global $error = ""
_CommSetPort(7, $error)
if($error <> "") then
MsgBox(0, "", $error)
EndIf

Example()

Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted, $button
    Local $msg, $recv
	Local $parameterarray[3]   ; ADDED-------------------------------------------------------------


    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Create a Listening "SOCKET".
    ;   Using your IP Address and Port 33891.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    ; If the Socket creation fails, exit.
    ;If $MainSocket = -1 Then Exit


    ; Create a GUI for messages
    ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Wait for and Accept a connection
    ;==============================================
    Do
		exit_server()
        $ConnectedSocket = TCPAccept($MainSocket) ;receiving input over internet
    Until $ConnectedSocket <> -1

	TCPSend($ConnectedSocket, "Hello World")
    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1
        exit_server()
        ; GUI Closed
        ;--------------------
        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then
			$ConnectedSocket = -1
			Do
				exit_server()
				$ConnectedSocket = TCPAccept($MainSocket)
			Until $ConnectedSocket <> -1
		EndIf

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then
			GUICtrlSetData($edit, $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
			_CommSendstring(GUICtrlRead($recv) & @CR)
			Sleep(100)
			MsgBox(0, "", _CommGetstring())
		EndIf

		;added----------------------------------------------------------------------------------------
		_CommSendstring("read",1)
		Sleep(1)
		$parameterarray[0]=_CommGetstring()
		Sleep(1)
		$parameterarray[1]=_CommGetstring()
		Sleep(1)
		$parameterarray[2]=_CommGetstring()
		Sleep(1)
		$parameterarray[3]=_CommGetstring()
		Sleep(1)
		;----------------------------------------------------------------------------------------------
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

Func exit_server()
	Local $msg1

	$msg1 = GUIGetMsg()

	if($msg1 = $GUI_EVENT_CLOSE) then Exit
EndFunc